//
//  WebSocketUtil.m
//  Mahjong
//
//  Created by 饶首建 on 17/5/23.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "WebSocketUtil.h"

#import "SRWebSocket.h"
#import "SocketIO.h"

#import "Mahjong-swift.h"

@interface WebSocketUtil()<SocketIODelegate>

@property (nonatomic,strong) SocketIOClient *socketio;

@end

static WebSocketUtil *wu;

@implementation WebSocketUtil

+ (instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        wu = [[WebSocketUtil alloc]init];
    });
    return wu;
}

- (void)connect:(NSDictionary *)dict{
    if (!dict) {
        return;
    }
    NSURL* url = [[NSURL alloc] initWithString:BASE_SOCKETIO_URL];
    
//    { query: "matchId=abcdefg&playerName=dan" }
    
    _socketio = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @NO,
//                                                                       @"reconnectWait":@1,
//                                                                       @"reconnects":@NO,
                                                                       @"forcePolling":@YES,
                                                                       @"forceWebsockets":@YES,
                                                                       @"connectParams":dict
                                                                       }];
    //reconnects(Bool) // 是否重新连接 服务器失败. Default is `true`
    //forcePolling(Bool)// 是否使用 xhr-polling. Default is `false`
    
//    [_socketio emit:@"gameTest" with:@[@"testString"]];//发消息
    
    [_socketio on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
        Log(@"socket connected------>");
        if (_delegate && [_delegate respondsToSelector:@selector(connectCallBack:)]) {
            [_delegate connectCallBack:ack];
        }
    }];
    
    [_socketio on:@"error" callback:^(NSArray * data, SocketAckEmitter * ack) {
        Log(@"error------>%@",data);
        if (_delegate && [_delegate respondsToSelector:@selector(errorCallBack:)]) {
            [_delegate errorCallBack:ack];
        }
    }];
    [_socketio on:@"disconnect" callback:^(NSArray * data, SocketAckEmitter * ack) {
        Log(@"disconnect------>%@",data);
        if (_delegate && [_delegate respondsToSelector:@selector(disConnectCallBack:)]) {
            [_delegate disConnectCallBack:ack];
        }
    }];
    
    [_socketio on:@"game" callback:^(NSArray * data, SocketAckEmitter * ack) {
        Log(@"game------>%@",data);
        if (data && data.count && _delegate && [_delegate respondsToSelector:@selector(actionCallBack:)]) {
            [_delegate actionCallBack:data[0]];
        }
    }];
    [_socketio on:@"match" callback:^(NSArray * data, SocketAckEmitter * ack) {
        Log(@"match------>%@",data);
        if (data && data.count && _delegate && [_delegate respondsToSelector:@selector(matchCallBack:)]) {
            [_delegate matchCallBack:data[0]];
        }
    }];
    [_socketio on:@"newGame" callback:^(NSArray * data, SocketAckEmitter * ack) {
        Log(@"newGame------>%@",data);
        if (data && data.count && _delegate && [_delegate respondsToSelector:@selector(newGameCallBack:)]) {
            [_delegate newGameCallBack:data[0]];
        }
    }];
    [_socketio on:@"me" callback:^(NSArray * data, SocketAckEmitter * ack) {
        Log(@"me------>%@",data);
        if (data && data.count && _delegate && [_delegate respondsToSelector:@selector(meCallBack:)]) {
            [_delegate meCallBack:data[0]];
        }
    }];
    [_socketio on:@"matchLeave" callback:^(NSArray * data, SocketAckEmitter * ack) {
        Log(@"matchLeave------>%@",data);
        if (data && data.count && _delegate && [_delegate respondsToSelector:@selector(matchLeaveCallBack:)]) {
            [_delegate matchLeaveCallBack:data[0]];
        }
    }];
    [_socketio on:@"matchWaiting" callback:^(NSArray * data, SocketAckEmitter * ack) {
        Log(@"matchWaiting------>%@",data);
        if (data && data.count && _delegate && [_delegate respondsToSelector:@selector(matchWaitCallBack:)]) {
            [_delegate matchWaitCallBack:data[0]];
        }
    }];
    [_socketio on:@"matchReconnect" callback:^(NSArray * data, SocketAckEmitter * ack) {
        Log(@"matchReconnect------>%@",data);
        if (data && data.count && _delegate && [_delegate respondsToSelector:@selector(matchReconnectCallBack:)]) {
            [_delegate matchReconnectCallBack:data[0]];
        }
    }];
    [_socketio on:@"matchBack" callback:^(NSArray * data, SocketAckEmitter * ack) {
        Log(@"matchBack------>%@",data);
        if (data && data.count && _delegate && [_delegate respondsToSelector:@selector(matchBackCallBack:)]) {
            [_delegate matchBackCallBack:data[0]];
        }
    }];
    [_socketio on:@"matchChangeSeat" callback:^(NSArray * data, SocketAckEmitter * ack) {
        Log(@"matchChangeSeat------>%@",data);
        if (data && data.count && _delegate && [_delegate respondsToSelector:@selector(matchChangeSeatCallBack:)]) {
            [_delegate matchChangeSeatCallBack:data[0]];
        }
    }];
    
    [_socketio connect];
}

- (void)close{
    [_socketio disconnect];
}

- (void)reconnect{
    [_socketio reconnect];
}


- (void)send:(NSString*)eventName things:(NSArray*)array{
    [_socketio emit:eventName with:array];//发消息
}

- (void)dealloc{
    Log(@"");
}

@end
